//
//  ZLStatusListDAL.swift
//  ZLWeibo
//
//  Created by zhanglongtao on 17/1/18.
//  Copyright © 2017年 hanju001. All rights reserved.
//

import Foundation

//DAL data access Layer 数据访问层
//使命： 负责处理数据库和网络数据，给listViewModel返回的[字典数组]
//在调整系统的时候，尽量做最小的调整
class ZLStatusListDAL {
    
    //参数之所以参照网络接口，就是为了对原有代码的最小化调整
    class func loadStatus(since_id: Int64,max_id: Int64, completion: @escaping (_ list: [[String: AnyObject]], _ isSuccess: Bool)->()) {
        
        //检查本地数据 如果有直接返回
        guard let userId = ZLNetWorkManager.shared.userAcount.uid else{
            
            return
        }
        let array = ZLSQLiteManager.shared.loadStatus(userId: userId, since_id: since_id, max_id: max_id)
        //判断数组的数量
        if array.count > 0{
            
            completion(array, true)
        }
        
        //加载网络数据
        ZLNetWorkManager.shared.statusList(since_id: since_id, max_id: max_id) { (list, isSuccess) in
            
            if !isSuccess{
                completion(list, false)
                
                return
            }
            //完成之后  写入数据库
            ZLSQLiteManager.shared.updateStatus(userId: userId, array: list)
            
            //返回网络数据
            completion(list, isSuccess)
            
        }
        
       
        
        
    }
    
}
